<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单区域 -->
      <el-form class="login-form" :rules="rules" :model="user" ref="form" :hide-required-asterisk="true">
        <el-form-item prop="mobile" >
          <el-input v-model="user.mobile" ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="user.code"></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
          <el-checkbox
            name="type"
            label="我已阅读并同意用户协议和隐私条款"
            v-model="user.isAgree"
          ></el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="isLoading"
            @click="hSubmit"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rules: {
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            pattern: /^1[345678]\d{9}$/,
            message: '请输入合法的手机号码',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          {
            pattern: /^\d{6}$/,
            message: '请输入合法的验证码',
            trigger: 'blur'
          }
        ],
        isAgree: [
          {
            // 自定义规则
            validator: function (rule, value, callback) {
              if (value) {
                // 选中
                callback();
              } else {
                // 用户不同意
                callback(new Error('你必须要同意！'));
              }
            },
            trigger: 'change'
          }
        ]
      },
      user: {
        isAgree: false,
        mobile: '13911111111', // 手机号
        code: '246810' // 密码
      },
      isLoading: false
    };
  },
  methods: {
    async doLogin () {
      const { mobile, code } = this.user;
      this.isLoading = true;
      try {
        const res = await this.$axios({
          method: 'POST',
          url: '/mp/v1_0/authorizations',
          data: {
            mobile,
            code
          }
        });
        this.isLoading = false;
        console.log(res);
        localStorage.setItem('tokenStr', res.data.data.token);
        this.$message({
          message: '恭喜你，登录成功',
          type: 'success'
        });
        this.$router.push('/');
        // await: 取出promise中的then(res) 中的结果
        console.log(res);
      } catch (err) {
        // err 就相当于promise中的catch(err)
        console.log(err);
        this.isLoading = false;
        this.$message.error('登录失败，请重新登录');
      }
    },
    // doLogin () {
    //   const { mobile, code } = this.user;
    //   this.isLoading = true;
    //   this.$axios({
    //     method: 'POST',
    //     url: '/mp/v1_0/authorizations',
    //     data: {
    //       mobile,
    //       code
    //     }
    //   }).then(res => {
    //     this.isLoading = false
    //     console.log(res);
    //     localStorage.setItem('tokenStr', res.data.data.token)
    //     this.$message({
    //       message: '恭喜你，登录成功',
    //       type: 'success'
    //     })
    //     this.$router.push('/')
    //   }).catch(() => {
    //     this.isLoading = false
    //     this.$message.error('登录失败，请重新登录')
    //   })
    // },
    hSubmit () {
      this.$refs.form.validate((valid) => {
        // valid 就是表单验证的结果，如果是true，表示通过了
        // console.log(valid)
        if (valid) {
          this.doLogin();
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
